public class MergeTwoSortedLists {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode virtualNode = new ListNode();
        ListNode last = virtualNode; // 尾指针(尾插法)
        ListNode cur1 = list1, cur2 = list2;

        // 合并
        while(cur1 != null && cur2 != null) {
            if(cur1.val < cur2.val) {
                last.next = cur1;
                last = last.next;
                cur1 = cur1.next;
            }else {
                last.next = cur2;
                last = last.next;
                cur2 = cur2.next;
            }
        }
        // 可能存在一方链表没有遍历完(该链表元素普遍较大)
        if(cur1 != null) {
            last.next = cur1;
        }
        if(cur2 != null) {
            last.next = cur2;
        }

        return virtualNode.next;
    }
}
